/*  Selector: an object-oriented Javascript library, version 1.2.0
 *  (c) 2005 Anping Wang <timawang@google.com>
 *
 *  THIS FILE IS AUTOMATICALLY GENERATED. When sending patches, please diff
 *  against the source tree, available from the Selector. 
 *
 *  Selector is freely distributable under the terms of an MIT-style license.
 *
 *  For details, see the Selector web site: http://www.selector.us
 *
/*--------------------------------------------------------------------------*/

/**
  Including this file to make any absolute element draggable. Simply
  add attribute resizable="ture" and draggable="true"
  to your absolute element. 
  
  For example
  <div id="test" style="position:absolute;left:120px;top:200px;border:1px green solid;" 
    draggable="true" resizable="true" >
    This is a draggable element
  </div>
  
  to drag a different absolute element, use 
    selector:draggable="true" selector:dragElement="test" 
    
  to drag within a container use
    selector:draggable="true" selector:dragElement="test" selector:dragBox="container"
  
  Currently, only tested for IE 6.0 and Firefox 1.0 preview

  You can also assign event handlers to dragging and resizing event using following
  attribute
     onDrag_End
     onDrag_Drag
     onDrag_Begin

     onResize_Resize
     onResize_Begin
     onResize_End
     
  All this event handler are called with two parameters:
     e and selected element
**/


function JBSelector()
{
  this.scrollTop=0;
  this.onSelect = null;
  this.onDeselect = null;
  
  this.checkVersion = function()
  {
    var agt=navigator.userAgent.toLowerCase();
    var major = parseInt(navigator.appVersion);
    
    var supported = agt.indexOf('firefox')!=-1
      ||agt.indexOf('msie 6.0')!=-1
    if(!supported)
    {
      //alert('this application only tested from Internet Explorer 6.0 and Firefox 1');
    }
    return supported;
  }
  
  this.minimumSize = 10;
  this.dragBorderWidth = 5;
  //add dagging div to cell
  this.onDropping = new Function();
  this.onChange = null;
  this.container = null;
  this.clone = false;
  this.dragElement=null;
  this.constrain= null;
  //work out the new rect with respect to the container
  this.allowedRect = function (originalRect)
    {
      var containingRect = 
      {
        left : this.container.left ,
        top : this.container.top,
        width: this.container.width,
        height : this.container.height
      }
      
      if(this.dragElement.parentNode && this.dragElement.parentNode==this.constrain 
        && (this.constrain.style.position=='relative' || this.constrain.style.position=='absolute'))
      {
          containingRect.left=0;
          containingRect.top=0;
      }
      
      var newRect = 
      {
        left : Math.max(originalRect.left, containingRect.left),
        top : Math.max(originalRect.top, containingRect.top),
        width: Math.min(originalRect.width,containingRect.width),
        height:Math.min(originalRect.height,containingRect.height)
      }
      
     
      if(javaBaseBrowser.ie )
      {
        with(newRect)
        {   
          width = width + parseInt(this.dragElement.style.borderLeftWidth?this.dragElement.style.borderLeftWidth:0)
            + parseInt(this.dragElement.style.borderRightWidth?this.dragElement.style.borderRightWidth:0);
          height= height + parseInt(this.dragElement.style.borderTopWidth?this.dragElement.style.borderTopWidth:0)
            + parseInt(this.dragElement.style.borderBottomWidth?this.dragElement.style.borderBottomWidth:0);
        }
      }
      
      with(containingRect)
      {
        left = left 
          - parseInt(this.dragElement.style.borderLeftWidth?this.dragElement.style.borderLeftWidth:0)
          - parseInt(this.dragElement.style.paddingRight?this.dragElement.style.paddingRight:0)
          - parseInt(this.dragElement.style.borderRightWidth?this.dragElement.style.borderRightWidth:0)
          - parseInt(this.dragElement.style.paddingRight?this.dragElement.style.paddingRight:0);
        top= top 
          - parseInt(this.dragElement.style.borderTopWidth?this.dragElement.style.borderTopWidth:0) 
          - parseInt(this.dragElement.style.paddingTop?this.dragElement.style.paddingTop:0) 
          - parseInt(this.dragElement.style.borderBottomWidth?this.dragElement.style.borderBottomWidth:0)
          - parseInt(this.dragElement.style.paddingBottom?this.dragElement.style.paddingBottom:0);
      }
       
      
      rect = 
      {
        left : Math.min(newRect.left, containingRect.left+containingRect.width-newRect.width),
        top : Math.min(newRect.top, containingRect.top+containingRect.height-newRect.height),
        width: Math.min(newRect.width,containingRect.width),
        height:Math.min(newRect.height,containingRect.height)
      }      
      return rect;
    };
    
  this.doMouseDown = function (e)
  {
    if(this.draggingMode ==-1) return true;

    var ele = JavaBase.EventFactory.getEventSource(e);
    
    if(ele == null) return true; 
    
    this.select(e, ele);
    
    if(this.draggingMode==10)
    {
        if(this.onDrag_Begin)
        try
        {
          this.onDrag_Begin(e, this.dragElement);
        }catch(e)
        {
         
        }
      }else
      {
        if(this.onResize_Begin)
        try
        {
          this.onResize_Begin(e, this.dragElement);
        }catch(e)
        {
          
        }
      } 
    return false;
  }
  
  //can be a mouse down function to pick up an element to move
  this.pick=function(e, ele, options)
  {
    this.draggingMode=10;
    this.selectedDraggable=true;    
    
    this.options= options;
    
    this.select(e, ele);      
    if(this.draggingMode==10)
    {
        if(this.onDrag_Begin)
        try
        {
          this.onDrag_Begin(e, this.dragElement);
        }catch(e)
        {
         
        }
      }else
      {
        if(this.onResize_Begin)
        try
        {
          this.onResize_Begin(e, this.dragElement);
        }catch(e)
        {
          
        }
      } 
  }
  
  //private function
  this.select = function (e, ele)
  {
    Event.stop(e);    
    ele = $(ele);
    this.style = ele.style;
    
    this.constrain = $(JavaBase.DomUtil.getAttributeValue(ele, 'dragBox'));
    this.vBorderTop = 0;
    this.vBorderLeft = 0;
    this.vBorderRight = 0;
    this.vBorderBottom = 0;
    if(this.constrain)
    {
      this.vBorderTop = this.constrain.style.borderTopWidth ?  parseInt(this.constrain.style.borderTopWidth):0;
      this.vBorderLeft = this.constrain.style.borderLeftWidth ?  parseInt(this.constrain.style.borderLeftWidth):0;
      this.vBorderRight = this.constrain.style.borderRightWidth ?  parseInt(this.constrain.style.borderRightWidth):0;
      this.vBorderBottom = this.constrain.style.borderBottomWidth ?  parseInt(this.constrain.style.borderBottomWidth):0;
      this.container = JavaBase.Dimension.getInnerElementRect(this.constrain);
      if(javaBaseBrowser.ie)
      {
        with (this.container)
        {
            width = width + this.vBorderLeft+this.vBorderRight;
            height = height + this.vBorderBottom+this.vBorderTop;
        }
      
      }
    }else
    {
      this.container = {left:0, top:0, width :10000, height:10000};
      this.constrain = document.body;
    }

    
    this.selected = ele;
    
    this.anchor = JavaBase.EventFactory.getPosition(e);
   
    this.dragElement = this.selected;
    
    var drag = JavaBase.DomUtil.getAttributeValue(this.selected, 'dragElement');
    
    if(this.draggingMode == 10 && drag != null)
    {
      this.dragElement = $(drag);
    }
    
    this.clone = JavaBase.DomUtil.getAttributeValue(this.selected, 'clone') == 'true';
    
    this.anchorRect = JavaBase.Dimension.getElementRect(this.dragElement);

    with(this.anchorRect)
    {
        left = left + this.vBorderLeft;
        top = top + this.vBorderTop;
    }
    
    if(this.clone)
    {
      var cloned = this.dragElement.cloneNode(true);
      cloned.id=this.dragElement.id + 'cloned';
      cloned.clonedFrom = this.dragElement;
       
      with (cloned.style)
      {
        left = (this.anchorRect.left) + 'px';
        top = (this.anchorRect.top) + 'px';

        width=this.anchorRect.width+'px';
        height=this.anchorRect.height+'px';
        position='absolute';
      }
      document.body.appendChild(cloned);
      this.dragElement = cloned;
    }
    
    this.dragElement.anchorRect = this.anchorRect;
    
    if(this.onSelect)
    {
      this.onSelect(this.selected);
    }
    if(this.dragElement)
    {
      this.dragElement.zOldIndex=this.dragElement.style.zIndex;    
      this.dragElement.style.zIndex='1000';    
    }
 
    var eh=JavaBase.DomUtil.getAttributeValue(this.selected, 'onDrag_End');
    if(eh) this.onDrag_End = eval(eh);

    eh=JavaBase.DomUtil.getAttributeValue(this.selected, 'onDrag_Begin');
  
    if(eh) this.onDrag_Begin = eval(eh);

    eh=JavaBase.DomUtil.getAttributeValue(this.selected, 'onDrag_Drag');

    if(eh) this.onDrag = eval(eh);
   
    eh=JavaBase.DomUtil.getAttributeValue(this.selected, 'onResize');
    
    if(eh) this.onResize = eval(eh);
      eh=JavaBase.DomUtil.getAttributeValue(this.selected, 'onResize_Begin');
    
    if(eh) this.onResize_Begin = eval(eh);
      eh=JavaBase.DomUtil.getAttributeValue(this.selected, 'onResize_End');
    
    if(eh) this.onResize_End = eval(eh);
    if(JBRectTool && this.selected && this.selected.shadow) {
        JBRectTool.hideShadow(this.selected);
    }
    if(this.options && this.options.opacity) {
        this.oldOpacity = this.dragElement.getInlineOpacity();
        if(this.options.opacity == true)//true for default 0.6
            this.dragElement.setOpacity(0.6);
        else
            this.dragElement.setOpacity(this.options.opacity);
    }
    if(e.preventDefault)
      e.preventDefault();        
  }
  this.setRect = function(newRect)
  {
    left = newRect.left;
    if(this.container)
    {
      newRect = this.allowedRect(newRect);
    }
    
    with(this.dragElement.style)
    {
      left=(newRect.left) +'px';
      top=(newRect.top ) +'px';
      
      if(this.draggingMode!=10)
      {
        width=(newRect.width) +'px';
        height=(newRect.height) +'px';
      }
    }
  }
    
  //move the element
  this.doMove = function(e)
  {
      pos = JavaBase.EventFactory.getPosition(e);
      this._doMove(e, pos);
  }
  
  this._doMove = function(e,pos)
  {
      if(this.draggingMode == 10)
      {
        
        dx = this.anchor.left - pos.left;
        dy = this.anchor.top - pos.top;
        
        newRect = JavaBase.Dimension.getInnerElementRect(this.dragElement);
      
        with(newRect)
        {
          left=(this.anchorRect.left - dx);
          top=(this.anchorRect.top - dy);
        }
         
        if(!this.clone)
          if(this.dragElement.style.position=='relative' || this.dragElement.style.position=='absolute')
          {
            newRect.left = newRect.left - this.container.left;
            newRect.top = newRect.top - this.container.top;
          }
          
        this.setRect(newRect);
         
        if(this.onDrag)
        try
        {
          this.onDrag(e, this.dragElement);
        }catch(e)
        {
           
        }
      }
    }
    
    //resize the element
  this.doResize = function(e)
    {
      pos = JavaBase.EventFactory.getPosition(e);
      this._doResize(e, pos);
    }
    
  this._doResize = function(e, pos)
    {
    
      dx =this.anchor.left - pos.left;
      dy =this.anchor.top - pos.top;

      newRect = JavaBase.Dimension.getInnerElementRect(this.dragElement);
      
      with(newRect)
      {
        left = left - parseInt(this.dragElement.style.borderLeftWidth?this.dragElement.style.borderLeftWidth:0);
        top = top - parseInt(this.dragElement.style.borderTopWidth?this.dragElement.style.borderTopWidth:0);
      }
  
      newRect =
      {
        left : this.anchorRect.left,// - this.vBorderLeft,
        top : this.anchorRect.top,// - this.vBorderTop,
        width : this.anchorRect.width - this.vBorderLeft - this.vBorderRight,
        height : this.anchorRect.height
      }
        if(this.clone)
        {
          if(this.dragElement.clonedFrom.style.position=='relative' || this.dragElement.clonedFrom.style.position=='absolute')
          {
            newRect.left = newRect.left - this.container.left;
            newRect.top = newRect.top - this.container.top;
          }
        }else
        {
          if(this.dragElement.style.position=='relative' || this.dragElement.style.position=='absolute')
          {
            newRect.left = newRect.left - this.container.left;
            newRect.top = newRect.top - this.container.top;
          }
        
        }
      
      if(this.draggingMode == 1)
      {
        var w = newRect.width + dx;
        if(w > this.minimumSize)
        {
          with(newRect)
          {
            width=w ;
            left=(newRect.left - dx);
          }
        }
      }
      
      if(this.draggingMode == 2)
      {
        w = newRect.width - dx;
        if(w > this.minimumSize)
        {
          with( newRect)
          {
            width=w ;
          }
        }
      }
      
      if(this.draggingMode == 3)
      {
        var h = newRect.height + dy;
        if(h > this.minimumSize)
        {
          with( newRect)
          {
            top = (newRect.top - dy) ;
            height = h;
          }
        }
      }
      
      if(this.draggingMode == 4)
      {
        h = newRect.height - dy;
        
        if(h > this.minimumSize)
        {
          with( newRect)
          {
            height=h ;
          }
        }
      }

      if(this.draggingMode == 6)
      {
        w = newRect.width + dx;
        h = newRect.height + dy;
        
        if(h > this.minimumSize && w >  this.minimumSize )
        {
          with( newRect)
          {
            height=h;
            width=w ;
            left= (newRect.left - dx) ;
            top = (newRect.top - dy);
          }
        }
      }

      if(this.draggingMode == 7)
      {
        w = newRect.width + dx;
        h = newRect.height - dy;
        
        if(h > this.minimumSize && w >  this.minimumSize )
        {
          with( newRect)
          {
            height=h  ;
            width=w ;
            left=(newRect.left - dx)  ;
          }
        }
      }
      
      if(this.draggingMode == 8)
      {
        w = newRect.width - dx;
        h = newRect.height - dy;
        
        if(h > this.minimumSize && w >  this.minimumSize )
        {
          with( newRect)
          {
            height=h  ;
            width=w  ;
          }
        }
      }
      
      if(this.draggingMode == 9)
      {
        w = newRect.width - dx;
        h = newRect.height + dy;
        
        if(h > this.minimumSize && w >  this.minimumSize )
        {
          with(newRect)
          {
            height=h  ;
            width=w  ;
            top = (newRect.top - dy)  ;
          }
        }
      }
      
      this.setRect(newRect);
        if(JB) {
            JB.stretch(this.dragElement);
        }
      if(this.onResize)
      try
      {
        this.onResize(e, this.dragElement);
      }catch(e)
      {
         
      }
      
    }


  
  this.doMouseUp = function(e)
    {
      if(this.draggingMode==10)
      {
        if(this.onDrag_End)
        try
        {
          this.onDrag_End(e, this.dragElement);
        }catch(e)
        {
         
        }
      }else
      {
        if(this.onResize_End)
        try
        {
          this.onResize_End(e, this.dragElement);
        }catch(e)
        {
          
        }
      }
      
      try{
      
        if(this.clone && !(!this.dragElement))
        {
          newRect = JavaBase.Dimension.getInnerElementRect(this.dragElement);


          if(this.dragElement.clonedFrom.style.position=='relative' || this.dragElement.clonedFrom.style.position=='absolute')
          {
            newRect.left = newRect.left - this.container.left;
            newRect.top = newRect.top - this.container.top;
          }
  
          with(newRect)
          {
            left = left - parseInt(this.dragElement.style.borderLeftWidth?this.dragElement.style.borderLeftWidth:0);
            top = top - parseInt(this.dragElement.style.borderTopWidth?this.dragElement.style.borderTopWidth:0);
          }
          
          with(this.dragElement.clonedFrom.style)
          {
            left=newRect.left+'px';
            top=newRect.top+'px';
            if(this.draggingMode!=10)//change if only in resize mode
            {
                height=this.dragElement.style.height;
                width=this.dragElement.style.width;
            }
          }
        }
        
        if(this.dragElement!=null)
            this.dragElement.style.zIndex=this.dragElement.zOldIndex;    
        
        if(JB.synchDim && this.dragElement)
        {
          id = this.dragElement.id;
          if(this.clone)
            id =this.dragElement.clonedFrom.id;

          if(this.draggingMode==10)
          {
            JB.synchLocation(id);
          }
          else
            JB.synchSize(id);
        }
        
        if(JBRectTool && this.selected&& this.selected.shadow) {
            JBRectTool.shadowDiv(this.selected);
        }
        
        if(!this.clone &&this.selected!=null)
        {
            if(this.oldOpacity!=null)
                this.selected.setOpacity( this.oldOpacity);
        }
      }catch(e)
      {
        //dont do anything
      }
      this.options=null;
      this.init();
      return true;
    }

  this.init = function()
    {
      if(this.clone && this.dragElement)
      {
        try
        {
          document.body.removeChild(this.dragElement);
          }catch(e)
          {
           
          }
      }
      this.selected=null;
      this.draggingMode=-1;
      this.dragElement = null;
      this.onDrag_End = null;
      this.onDrag_Begin = null;
      this.onDrag = null;
      this.onResize = null;
      this.onResize_Begin = null;
      this.onResize_End = null;
    }
  //
  //this is called when mouse moves onto the element
  //
  this.doMouseMove = function(e, ele)
    {
      this.selectedResizable = JavaBase.DomUtil.getAttributeValue(ele, 'resizable')=='true';
      this.selectedDraggable = JavaBase.DomUtil.getAttributeValue(ele, 'draggable')=='true';
      if(!this.selectedResizable&&!this.selectedDraggable)
      {
        return;
      }

      if(this.orginalCursor==null)
           this.orginalCursor = ele.style.cursor;
      this.draggingMode =-1;
      pos = JavaBase.EventFactory.getPosition(e);
      rect = JavaBase.Dimension.getElementRect(ele, true);

      rect.left = rect.left - JavaBase.Dimension.getScrollLeft(ele)+this.vBorderLeft ;
      rect.top = rect.top - JavaBase.Dimension.getScrollTop(ele) + this.vBorderTop;
      
      if(this.selectedResizable)
      {
        var l = pos.left > rect.left && pos.left < rect.left+this.dragBorderWidth;
        var r = pos.left > rect.left+rect.width-this.dragBorderWidth && pos.left < rect.left + rect.width;
        var t = pos.top > rect.top && pos.top < rect.top+this.dragBorderWidth;
        var b = pos.top > rect.top+rect.height-this.dragBorderWidth && pos.top < rect.top+rect.height;
        
        if(l&&t)
        {
          ele.style.cursor="se-resize";
          this.draggingMode=6;
        }else if(l&&b)
        {
          ele.style.cursor="ne-resize";
          this.draggingMode=7;
        }else if(r&&b)
        {
          ele.style.cursor="nw-resize";
          this.draggingMode=8;
        }else if(r&&t)
        {
          ele.style.cursor="sw-resize";
          this.draggingMode=9;
        }else if(l)
        {
          ele.style.cursor="e-resize";
          this.draggingMode=1;
        }else if(r)
        {
          ele.style.cursor="e-resize";
          this.draggingMode=2;
        }else if(t)
        {
          ele.style.cursor="n-resize";
          this.draggingMode=3;
        }else if(b)
        {
          ele.style.cursor="n-resize";
          this.draggingMode=4;
        }else
        {
          ele.style.cursor= this.orginalCursor;
          this.draggingMode=-1;
        }
      }
      
      if(this.draggingMode==-1)
      {
        this.selectedDraggable = JavaBase.DomUtil.getAttributeValue(ele, 'draggable')=='true';
        if(this.selectedDraggable)
        {
       //     ele.style.cursor= this.orginalCursor;
            this.draggingMode=10;    
        }
      }
      return this.size!=-1;
    }
  
  this._doMouseMove = function(e)
    {
      if(this.selected !=null)
      {
        Event.stop(e);
        if(this.draggingMode != 10 && this.selectedResizable)
        {
          this.doResize(e);
        }else if(this.selectedDraggable)
        {
          this.doMove(e);
        }
      
        var pos = JavaBase.EventFactory.getPosition(e);
        var winRect= JavaBase.Dimension.getWindowRect();
        
        if(this.onChange)
            this.onChange(e);
    /*
        if(pos.top > winRect.height - 20)
        {
          x=0;
          while(scrollTop < winRect.height)
          {
            scrollTop +=5
            x+=5;
            window.scroll(0,scrollTop);
            this._doMove({left:pos.left, top:pos.top+scrollTop});
            
          }
          this.onMouseUp(e);
        }else if(pos.top < 10)
        {
          x=0;
          while(scrollTop >0)
          {
            scrollTop -= 5;
            x -= 5;
            window.scroll(0,window.scrollTop);          
            this._doMove({left:pos.left, top:pos.top + x});
          }
          this.onMouseUp(e);
        } */
        return false;
      } 

      var ele = JavaBase.EventFactory.getEventSource(e);
      if(ele)
      {        
        if(this.doMouseMove(e, ele))
        {
          //Event.stop(e);
          return true;
        }
      }
      return true;
    }

  // mouseup event handler
  this.onMouseUp = function(e)
  {
    return _selector.doMouseUp(e);
  }
  //this mouse event handler
  this.onMouseMove = function(e)
  { 
    return _selector._doMouseMove(e);
  }
  
  // mousedown event handler
  this.onMouseDown = function(e)
  {
    return _selector.doMouseDown(e); 
  }  
}

var _selector = new JBSelector();

JavaBase.EventFactory.registerMousemoveListener(document, _selector.onMouseMove);
JavaBase.EventFactory.registerMouseupListener(document, _selector.onMouseUp);
JavaBase.EventFactory.registerMousedownListener(document, _selector.onMouseDown);
JavaBase.EventFactory.registerWindowLoadListener(_selector.checkVersion);
